package com.vfashion.dao.factory;

import java.sql.Connection;
import com.vfashion.dao.ClienteDAO;
import com.vfashion.dao.PaqueteDAO;
import com.vfashion.dao.VentaDAO;
import com.vfashion.dao.impl.mysql.MysqlClienteDAO;
import com.vfashion.dao.impl.mysql.MysqlPaqueteDAO;
import com.vfashion.dao.impl.mysql.MysqlVentaDAO;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author danjoas
 */

public class DAOFactory  {

    private static Connection conexion = null;
    private static DAOFactory daofactory = null;

    private DAOFactory() {
        /* pool de conexiones */
//        try {
//            Context ctx = new InitialContext();
//            //Se obtiene a travez de su nombre al pool de conexiones
//            DataSource ds = (DataSource) ctx.lookup("jdbc/vfashion");
//            //Se obtiene una conexion del pool
//            conexion = ds.getConnection();
//            System.out.println("Conexion OK");
//        } catch (SQLException ex) {
//            System.out.println("Error datasource :  " + ex.getMessage());
//
//        } catch (NamingException ex) {
//            System.out.println("Error Naming Exception datasource :  " + ex.getMessage());
//        }
        /*  conexion unica */
        String driver = "com.mysql.jdbc.Driver";
        String jdbc = "jdbc:mysql:";
        String ruta = "//localhost/";
        String nombreBD = "vfashion";
        String url = jdbc + ruta + nombreBD;
        String user = "root";
        String pass = "";
        try {
            Class.forName(driver);
            conexion = DriverManager.getConnection(url, user, pass);
            System.out.println("Conexion ok");
        } catch (Exception ex) {
            System.out.println("Errrr al cargar driver JDBC");
            System.out.println(ex.getMessage());
        }

    }

    public static DAOFactory getInst() {
        if (daofactory == null) {
            daofactory = new DAOFactory();
        }
        return daofactory;
    }

    public Connection getConexion() {
        try {
            if (conexion == null || conexion.isClosed()) {
                daofactory = new DAOFactory();
            }
        } catch (SQLException ex) {
            System.out.println("Error conexion cerrada : " + ex.getMessage());
            ex.printStackTrace();
        }
        return conexion;
    }

    public ClienteDAO getClienteDAO() {
        return new MysqlClienteDAO();
    }

    public PaqueteDAO getPaqueteDAO(){
        return new MysqlPaqueteDAO();
    }

    public VentaDAO getVentaDAO(){
        return new MysqlVentaDAO();
    }
}
